using ProtoModels;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

public class UIWinDialog : MonoBehaviour
{
	public delegate void WinDialogDelegate();

	public GameObject dialog;

	public GameObject winDialogCareer;

	public GameObject winDialogLeague;

	public UILabel header;

	public List<UISprite> stars = new List<UISprite>();

	public UILabel dominate;

	public UILabel opponentName;

	public UISprite opponentFlag;

	public UILabel careerText;

	public UILabel leagueText;

	public UILabel okText;

	public UILabel leadScore;

	public UILabel leadMessage;

	public UILabel leadWonScore;

	public UILabel countryPoints;

	public GameObject countryPointsGameObject;

	public GameObject shareButton;

	public UILabel shareLabel;

	private int startScore;

	private int endScore;

	private float time;

	private float duration;

	private bool animateScoreActive;

	private UILabel fadeOutLabel;

	private UILabel fadeInLabel;

	private bool activateCrossFadeLabels;

	private float crossTime;

	private float crossFadeDuration;

	private float crossInitialDelay;

	private float crossDelay;

	protected WinDialogDelegate onOk;

	protected string shareTitle;

	protected string shareSubtitle;

	protected string shareText;

	protected bool shareIsGroupPassed;

	protected void Show(GameObject dialogObj)
	{
		dialog.SetActive(value: true);
		if (!AdManager1.instance.ShieldAddress)
		{
			okText.parent.gameObject.SetActive(false);
			CheckTouch.Show(delegate
			{
				next(dialogObj);
				okText.parent.gameObject.SetActive(true);
			});
		}
		else
		{
			AdManager.single.ShowNative();
			next(dialogObj);
		}
	}

	private void next(GameObject dialogObj)
	{
		winDialogCareer.SetActive(dialogObj == winDialogCareer);
		winDialogLeague.SetActive(dialogObj == winDialogLeague);
	}

	public void HideShareButton()
	{
		shareButton.SetActive(value: false);
	}

	public void ShowShareButton(string title, string subtitle, string text, bool isGroupPassed = false)
	{
		if (!GGFacebook.instance.isAvailable())
		{
			HideShareButton();
			return;
		}
		/* shareIsGroupPassed = isGroupPassed;
		shareTitle = title;
		shareSubtitle = subtitle;
		shareText = text;
		shareLabel.text = GGFormat.RandomFrom("Share This!", "Let Friends Know!", "Brag!", "Share");
		shareButton.SetActive(value: true); */
	}

	public void ShowTournamentDialog(Tournament.TournamentResult result, WinDialogDelegate onComplete)
	{
		onOk = onComplete;
		Tournament tournament = result.tournament;
		Show(winDialogLeague);
		if (tournament.isTournamentComplete())
		{
			int num = tournament.humanRanking();
			if (num < 2)
			{
				header.text = "恭喜你获得奖牌！";
			}
			else
			{
				header.text = "锦标赛完成";
			}
			ShowShareButton(num + 1 + " Place in " + TournamentController.tournamentName((TournamentController.Tournaments)tournament.TournamentType()), "Can you beat my score!", "Just Won " + (num + 1) + " Place in " + TournamentController.tournamentName((TournamentController.Tournaments)tournament.TournamentType()));
		}
		else
		{
			header.text = "下一场 " + tournament.NameForCurrentRound();
			shareButton.SetActive(value: false);
		}
		if (tournament.isTournamentComplete())
		{
			int position = tournament.humanRanking();
			leagueText.text = "锦标赛结束！ " + TournamentController.NameForPosition(position);
		}
		else
		{
			leagueText.text = "下一场 " + tournament.NameForCurrentRound();
		}
		okText.text = "确定";
		UITools.ChangeText(leadWonScore, "+" + result.addedScore.ToString() + " 经验");
		animateScore(result.startingScore, result.startingScore + result.addedScore, 0.5f, 0.5f);
		ShowCountryScore(result.addedCountryScore);
	}

	public void ShowCountryScore(int countryScore)
	{
		if (countryScore <= 0)
		{
			countryPointsGameObject.SetActive(value: false);
			return;
		}
		//countryPointsGameObject.SetActive(value: true);
		//countryPoints.text = "国家积分： +" + countryScore;
	}

	public void ShowLeagueDialog(LeagueController.LeagueGameResult result, WinDialogDelegate onComplete)
	{
		bool isWin = result.isWin;
		int ballsWon = result.ballsWon;
		onOk = onComplete;
		Show(winDialogLeague);
		header.text = ((!isWin) ? "你输了！" : "你赢了！");
		string empty = string.Empty;
		LeagueController instance = LeagueController.instance;
		MyLeagueMemberDAO member = instance.HumanPlayer();
		int num = instance.RankForMember(member);
		empty = ((!isWin) ? (empty + GGFormat.RandomFrom("就差一点了！", "祝你下次好运！", "没关系，下一场你一定能赢！", "改进的机会还很多！")) : (empty + GGFormat.RandomFrom("精彩的比赛！", "这是一场重要的胜利！", "你展现了很大的潜力！")));
		string text = empty;
		empty = text + " 您在联赛中排名第 " + (num + 1) + " 。 你赢了 +" + ballsWon + " (球)";
		leagueText.text = empty;
		okText.text = "下一步";
		UITools.ChangeText(leadWonScore, "+" + result.addedScore.ToString() + " 经验");
		animateScore(result.startingScore, result.startingScore + result.addedScore, 0.5f, 0.5f);
		ShowShareButton(num + 1 + " 联赛名次", "你能超过我的分数吗？", ((!isWin) ? "已完成" : "胜利") + " 已 " + opponentName.text + " 争夺联赛第 " + (num + 1) + " 名！");
		ShowCountryScore(result.addedCountryScore);
	}

	public void ShowCareerDialog(CareerGameMode.GameComplete outcome, WinDialogDelegate onComplete)
	{
		int num = outcome.stars;
		CareerGameMode.CareerGroup group = outcome.opponent.group;
		CareerGameMode.CareerPlayer opponent = outcome.opponent;
		bool isGroupPassed = outcome.isGroupPassed;
		UITools.ChangeText(leadWonScore, "+" + outcome.addedScore.ToString() + " 经验");
		onOk = onComplete;
		Show(winDialogCareer);
		bool flag = num > 0;
		header.text = ((!flag) ? "你输了！" : "你赢了！");
		PlayerDeffinition.PlayerDef playerDef = PlayerDeffinition.instance.definitionForIndex(opponent.playerDef);
		GameConstants.SetFlag(opponentFlag, (int)playerDef.flag);
		opponentName.text = playerDef.name;
		string text = string.Empty;
		okText.text = "确定";
		if (isGroupPassed)
		{
			text = group.name + " 通过！";
			if (group.prizes.Count > 0)
			{
				StringBuilder stringBuilder = new StringBuilder();
				foreach (CareerGameMode.CareerPrize prize in group.prizes)
				{
					if (stringBuilder.Length > 0)
					{
						stringBuilder.Append(" 和 ");
					}
					stringBuilder.Append(prize.name);
				}
				stringBuilder.Append("!");
				text = text + " 您已解锁 " + stringBuilder.ToString();
				if (group.prizes.Count > 1)
				{
					okText.text = "解锁赢得的物品";
				}
				else
				{
					okText.text = "解锁 " + group.GetPrizeName();
				}
			}
		}
		else
		{
			switch (num)
			{
				case 0:
					text = GGFormat.RandomFrom("下次你会赢他的！", "下次祝你好运！", "你的比赛看起来很有希望，你能做到的！") + " 以 " + opponent.threeStars + " 分之差获胜！";
					break;
				case 1:
					text = GGFormat.RandomFrom("好技术，你正在走向称霸之路！", "你是一名技术娴熟的球员，很快就会成为明星！", "你展现出了潜力，你已经准备好称霸一切！") + " 以 " + opponent.threeStars + " 分之差获胜";
					break;
				case 2:
					text = GGFormat.RandomFrom("伟大的胜利！你们离称霸已经很近了！", "你真是太精彩了！再一点点就能称霸全场了！", "你的游戏潜力无限！你一定能称霸全场！") + " 以 " + opponent.threeStars + " 分的差距获胜，称霸全场！";
					break;
				case 3:
					text = GGFormat.RandomFrom("真是一次至高无上的胜利... 这场比赛太精彩了！", "你做到了！真是一场精彩的比赛，对手根本没有机会！", "人群为你辉煌的胜利欢呼雀跃！太棒了！！！");
					break;
			}
		}
		careerText.text = text;
		StartCoroutine(ShowStars(num));
		animateScore(outcome.startingScore, outcome.startingScore + outcome.addedScore, 0.5f, (float)outcome.stars * 0.5f + 0.5f);
		if (flag)
		{
			if (outcome.isGroupPassed && outcome.opponent.group.prizes.Count > 0)
			{
				ShowShareButton("已解锁 " + outcome.opponent.group.prizes[0].name, string.Empty, "战胜 " + playerDef.name + " 已解锁 " + outcome.opponent.group.prizes[0].name, isGroupPassed: true);
			}
			else
			{
				ShowShareButton("战胜 " + playerDef.name, string.Empty, "刚刚在我的羽毛球 3D 职业中战胜了 " + playerDef.name + "！ 试着打败我的分数吧。");
			}
		}
		else
		{
			HideShareButton();
		}
		ShowCountryScore(outcome.addedCountryScore);
	}

	public void animateScore(int from, int to, float duration, float delay)
	{
		this.duration = duration;
		startScore = from;
		endScore = to;
		UITools.ChangeText(leadScore, from.ToString() + " 经验");
		StartCoroutine(DoAnimateScore(from != to, delay));
	}

	private IEnumerator DoAnimateScore(bool animate, float delay)
	{
		yield return new WaitForSeconds(delay);
		animateScoreActive = animate;
	}

	private void Update()
	{
		if (animateScoreActive)
		{
			time += Time.deltaTime;
			float num = time / duration;
			UITools.ChangeText(text: ((int)Mathf.Lerp(startScore, endScore, num)).ToString() + " 经验", label: leadScore);
			if (num > 1f)
			{
				animateScoreActive = false;
				float num2 = 1.5f;
				leadScore.cachedTransform.localScale = new Vector3(num2, num2, 1f);
				TweenScale.Begin(leadScore.cachedGameObject, 0.2f, Vector3.one);
				activateCrossFadeLabels = true;
				crossInitialDelay = 0.5f;
				crossDelay = 1.5f;
				crossFadeDuration = 0.5f;
				fadeInLabel = leadMessage;
				fadeOutLabel = leadScore;
			}
		}
		if (activateCrossFadeLabels)
		{
			if (fadeInLabel == leadMessage && crossTime == 0f)
			{
				fadeInLabel.text = GGFormat.RandomFrom("排行榜", "世界排名", "世界位置", "比较您的得分！");
			}
			crossTime += Time.deltaTime;
			float t = (crossTime - crossInitialDelay) / crossFadeDuration;
			float num3 = Mathf.Lerp(0f, 1f, t);
			fadeInLabel.alpha = num3;
			fadeOutLabel.alpha = 1f - num3;
			if (crossTime > crossInitialDelay + crossFadeDuration + crossDelay)
			{
				crossTime = 0f;
				UILabel uILabel = fadeInLabel;
				fadeInLabel = fadeOutLabel;
				fadeOutLabel = uILabel;
			}
		}
	}

	private IEnumerator ShowStars(int numStars)
	{
		foreach (UISprite star2 in stars)
		{
			star2.color = Color.black;
		}
		bool isWin = numStars > 0;
		float duration = 0.15f;
		float delay = (!isWin) ? 0.05f : 0.3f;
		yield return new WaitForSeconds(delay);
		int starsCount = (!isWin) ? 3 : numStars;
		for (int i = 0; i < starsCount; i++)
		{
			UISprite star = stars[i];
			if (i < numStars)
			{
				star.color = Color.white;
				star.cachedTransform.localScale = new Vector3(2f, 2f, 1f);
			}
			else
			{
				star.color = Color.black;
				star.cachedTransform.localScale = new Vector3(1.3f, 1.3f, 1f);
			}
			TweenScale.Begin(star.cachedGameObject, duration, Vector3.one);
			yield return new WaitForSeconds(duration + delay);
		}
		if (numStars >= 3)
		{
			dominate.cachedGameObject.SetActive(value: true);
			dominate.cachedTransform.localScale = new Vector3(2f, 2f, 1f);
			Color color = dominate.color;
			color.a = 0f;
			dominate.color = color;
			TweenScale.Begin(dominate.cachedGameObject, duration, Vector3.one);
			TweenAlpha.Begin(dominate.cachedGameObject, duration, 1f);
		}
	}

	public void OnShare()
	{
		GGFacebook.instance.showShareDialog("Badminton 3D", shareTitle, shareSubtitle, shareText, GGSupportMenu.instance.appUrl(ConfigBase.instance.rateProvider, webFormat: true));
		Analytics.instance.shareFromWinDialog(MatchController.InitParameters.gameMode, shareIsGroupPassed);
	}

	public void OnOk()
	{
		dialog.SetActive(value: false);
		if (onOk != null)
		{
			onOk();
		}
	}

	public void OnLeaderboard()
	{
		SocialAuthentication.ShowLeaderboard();
	}
}
